﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class File_MD5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string msg = "";
        try
        {
           
            //从Request中取参数，注意上传的文件在Requst.Files中
            string name = Request["name"];
            string file_md5 = Request["file_md5"];
            int total = Convert.ToInt32(Request["total"]);
            int index = Convert.ToInt32(Request["index"]);
            long size_total = Convert.ToInt64(Request["size"]);
           int file_index = 0;//已存在文件part                              
            string dir = Server.MapPath("~/Upload/test1");
            string file_slice_dir = Server.MapPath("~/Upload/slices");
            string big_file = Path.Combine(dir, name);
            index += 1;//第一片；
            string file = Path.Combine(file_slice_dir, name + "_" + index);
            string flag = "";
            int shardSize = 2 * 1024 * 1024;		//以？MB为一个分片
            Big_file_upload temp_big_file = new Big_file_upload(); //自定义类
                                                                   //判断文件是否存在？
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);
            }                                         //判断文件是否存在？
            if (Directory.Exists(file_slice_dir) == false)
            {
                Directory.CreateDirectory(file_slice_dir);
            }
            if (System.IO.File.Exists(big_file) == true)//文件已经上传
            {
                FileInfo tempfileinfo = new FileInfo(big_file);//md5相同，文件大小相同，同一个文件，秒传
                if (tempfileinfo.Length == size_total) {
                    if (file_md5== temp_big_file.Get_file_md5(big_file, dir,shardSize))
                    {
                        file_index = total;
                    }
                }
            }
            if (file_index == total)
            {
                flag = "1";
            }
            else//判断文件已上传多少？
            {
                flag = "0";
                if (System.IO.File.Exists(file) == true)
                {
                    //判断第一片是否一致
                    if (file_md5 == temp_big_file.Get_file_md5(file, file_slice_dir, shardSize))
                    {
                        //获取已经上传的文件数
                        file_index = temp_big_file.File_part_count(file_slice_dir, name, total);
                    }
                }
            }
            //返回是否成功，是否是最后一片，文件是否存在，文件是否已经部分上传，上传到了多少片
            msg = flag + "|" + file_index ;
        }
        catch (Exception)
        {
            msg = "error" + "|0";
        }
      Response.Write(msg);
    }
  
}